import { Component, OnInit, ViewChild } from '@angular/core';
import { PersonalService } from '../../services/project/personal/personal.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
declare var CryptoJS: any;
import { ZzjFileUploader, FlowFileModel } from '@zzj/nag-file-uploader';
import { FILEURL } from './../../config';
import { personalModel } from './personal.model';
import { MessageService } from './../../services/message.service';

@Component({
  selector: 'app-personal',
  templateUrl: './personal.component.html',
  styleUrls: ['./personal.component.scss']
})
export class PersonalComponent implements OnInit {

  @ViewChild('imageUploader', { static: false }) imageUploader: ZzjFileUploader;

  constructor(
    private personalService: PersonalService,
    private message: NzMessageService,
    private router: Router,
    private messageService: MessageService
  ) { }

  public user: any = {};
  public oldPassword: string = '';
  public newPassword: string = '';
  public FILEURL = FILEURL;
  public imageQueue: any = [];
  public addimgStyle: any = {};
  public options: string[] = [];

  /**
   * 初始化
   */
  public async init() {
    const json = {
      MaxResultCount: 100,
      SkipCount: 0,
      Filter: JSON.parse(window.localStorage.getItem('APDInfo'))['userName']
    }
    const result = await this.personalService.getUser(json);
    this.user = result.result.items.find(it => it.userName === json.Filter);
    if (this.user.avatar) this.addimgStyle = { 'background-image': 'url(' + this.user.avatar + ')' };
  }

  /**
   * 上传
   * @param e 
   */
  public async fileChange(e) {
    this.imageUploader.add(e.target.files[0]);
  }

  public fileSuccess(transfer: FlowFileModel) {
    this.addimgStyle = {
      'background-image': 'url(' + transfer.url + ')'
    }
    this.user.avatar = transfer.url;
  }

  /**
   * 保存
   */
  public async preservation() {
    //手机号
    // let phoneNumber = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(isNaN(this.user.phoneNumber)){
      this.message.create('warning', '请输入数字');
      return;
    }

    if (this.user.phoneNumber.length != 11 ) {
      this.message.create('warning', '请输入正确的手机号');
      return;
    }
    //邮箱
    let email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (!email.test(this.user.emailAddress)) {
      this.message.create('warning', '请输入正确的邮箱地址');
      return;
    }

    const userModel = new personalModel(this.user);
    await this.personalService.editUser(userModel);
    this.messageService.send(this.addimgStyle);
    const info = JSON.parse(window.localStorage.getItem('APDInfo'));
    info.avatar = this.user.avatar;
    window.localStorage.setItem('APDInfo', JSON.stringify(info));
    this.message.create('success', `修改成功`);
  }

  //自动完成
  onChange(value): void {
    if (!value || value.indexOf('@') >= 0) {
      this.options = [];
    } else {
      this.options = ['qq.com', '163.com', 'gmail.com','Foxmail.com','139.com','sina.com','sina.cn'].map(domain => `${value}@${domain}`);
    }
  }

  /**
   * 返回
   */
  public comeBack() {
    this.router.navigate(['outer/dashboard']);
  }

  /**
   * 重置
   */
  public async reset() {
    await this.init();
    this.oldPassword = '';
    this.newPassword = '';
    this.options = [];
  }

  ngOnInit() {
    this.init();
  }

}
